package com.jeespring.admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeespring.common.constant.SystemConstant;
import com.jeespring.admin.dao.UserDao;
import com.jeespring.admin.entity.User;
import com.jeespring.admin.entity.UserExample;
import com.jeespring.admin.entity.UserExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @title:
 * @description:
 * @author: 张朝阳
 * @date: 2019/5/2 13:08
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public PageInfo findForPage(int currentPage, int pageSize){
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(SystemConstant.StatusType.VALID.getCode());
        PageHelper.startPage(currentPage, pageSize);
        List<User> userList = userDao.findForPage(example);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    public PageInfo getUserList(int currentPage, int pageSize){
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(SystemConstant.StatusType.VALID.getCode());
        PageHelper.startPage(currentPage, pageSize);
        List<User> userList = userDao.selectByExample(example);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    public void changeStatus(Long id, int status) {
        User user = userDao.selectByPrimaryKey(id);
        user.setStatus(status);
        userDao.updateByPrimaryKey(user);
    }
}
